/******************************************************************************
#ifdef DOC
File Name		: bsp_time.c
Description		: Cell Balance
Remark			: -
Date			: 2019/05/17
Copyright		: Techsum Power Technology Co., Ltd.
#endif
******************************************************************************/
/****************************************************************************
 include files
*****************************************************************************/
#include "sys_type.h"
#include "gd32f4xx.h"
#include "bsp_io.h"
#include "bsp_timer.h"
#include "bsp_wdg.h"
/****************************************************************************
 define macro
*****************************************************************************/

/****************************************************************************
 define type (STRUCTURE, UNION)
*****************************************************************************/
void vKeyCheck(UCHAR period);
void vUartRevOtCallback(void);
void vComLostOtCallback(UCHAR period);
void vLcdCtrlBuzzer(UCHAR period);

/****************************************************************************
 define type (Enumerated type ENUM)
*****************************************************************************/

/****************************************************************************
 Variable declaration
*****************************************************************************/
ULONG ulTimer5ms;
UCHAR ucTIM_50msFlg = TRUE;
UCHAR ucTIM_20msFlg;

volatile USHORT	unTIM_1msCounter;

/****************************************************************************
 Prototype declaration
*****************************************************************************/
/******************************************************************************
#ifdef DOC
FUNCTION		: vBSP_TimerInit
DESCRIPTION		: used for fullspeed timebase
INPUT			: none
OUTPUT			: none
UPDATE			: -
DATE			: 2019/6/4
******************************************************************************/
void vBSP_Timer1Init(void)
{
//    ULONG ulPrescalerValue = 0;
//	TIM_TimeBaseInitType TIM_TimeBaseStructure;
//	NVIC_InitType NVIC_InitStructure;
    
//    /* TIM2 clock enable */
//    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
    
//    /* Compute the prescaler value to have TIMx counter clock equal to 100 kHz */
//    ulPrescalerValue = (ULONG)(SystemCoreClock / 100000) - 1;
//    /* Time base configuration */
//    TIM_TimeBaseStructure.Period    = 500 - 1;	/*5MS*/
//    TIM_TimeBaseStructure.Prescaler = ulPrescalerValue;
//    TIM_TimeBaseStructure.ClkDiv    = 0;
//    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
//    TIM_InitTimeBase(TIM3, &TIM_TimeBaseStructure);
//    /* Prescaler configuration */
//    TIM_ConfigPrescaler(TIM3, ulPrescalerValue, TIM_PSC_RELOAD_MODE_IMMEDIATE);

//	/* Enable the TIM2 global Interrupt */
//	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
//	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//    NVIC_Init(&NVIC_InitStructure);

//    /*##-2- Start the TIM Base generation in interrupt mode ####################*/
//    /* Start Channel1 */
//    ucTIM_50msFlg = FALSE;
    
//    /* TIM2 enable update irq */
//    TIM_ConfigInt(TIM3, TIM_INT_UPDATE, ENABLE);
//    /* TIM2 enable counter */
//    TIM_Enable(TIM3, ENABLE);
}


/******************************************************************************
#ifdef DOC
FUNCTION		: vBSP_DelayMs
DESCRIPTION		: 
INPUT			: none
OUTPUT			: none
UPDATE			: -
DATE			: 2019/5/17
******************************************************************************/
void vBSP_DelayMs(ULONG usDly)
{
	for(USHORT idx = 0;idx < usDly;idx++)
	{
    	vSysTickDelay(0);	/*1ms*/
    	vBSP_WDG_Refresh();
	}
}
/****************************************************************************
#ifdef DOC
FUNCTION		: vTIM_1msStart
DESCRIPTION		: 1ms timer time setting
INPUT			: uctime[ms]
OUTPUT			: None
UPDATE			:
DATE			: 13/06/30
#endif
*****************************************************************************/
void vTIM_1msStart(USHORT usTime)
{
    unTIM_1msCounter = usTime;
}
/****************************************************************************
#ifdef DOC
FUNCTION		: vTIM_1msIsTimeout
DESCRIPTION		: Time-out confirmation of 1ms timer
INPUT			: None
OUTPUT			: 0:Unexpired 1:Time-out
UPDATE			:
DATE			: 13/06/30
#endif
*****************************************************************************/
UCHAR vTIM_1msIsTimeout(void)
{
    return (unTIM_1msCounter == 0);
}
/******************************************************************************
#ifdef DOC
FUNCTION		: HAL_TIM_PeriodElapsedCallback
DESCRIPTION		: 
INPUT			: none
OUTPUT			: none
UPDATE			: -
DATE			: 2019/5/17
******************************************************************************/

//void HAL_TIM_PeriodElapsedCallback(TIM_Module *htim)
//{
////    if(htim == TIM3)
////    {
////		ulTimer5ms+=5;

////		if( ulTimer5ms >= 50 )
////		{
////			ulTimer5ms = 0;
////			ucTIM_50msFlg = ON;
////		}

////		vKeyCheck(5);
////		//vComLostOtCallback(5);
////		vLcdCtrlBuzzer(5);
////    }
//}




/******************************************************************************
#ifdef DOC
FUNCTION		: HAL_SYSTICK_Callback
DESCRIPTION		: 
INPUT			: none
OUTPUT			: none
UPDATE			: -
DATE			: 2019/5/17
******************************************************************************/
void SYSTICK_Callback(void)
{
    if(unTIM_1msCounter > 0)
    {
        unTIM_1msCounter--;
    }
  //  vBOOT_1MsCallback();
   // FlashDriver1MsCallback();
   //vUartRevOtCallback();
}


__IO uint32_t uwTick;
void HAL_IncTick(void)
{
  uwTick++;
}
uint32_t HAL_GetTick(void)
{
  return uwTick;
}

 void vSysTickDelay(__IO uint32_t Delay)
{
  uint32_t tickstart = HAL_GetTick();
  uint32_t wait = Delay;
  
  /* Add a period to guarantee minimum wait */
  if (wait < 0xFFFFFFFFU)
  {
     wait++;
  }
  
  while((HAL_GetTick() - tickstart) < wait)
  {
  }
}


//void vBSP_PwmInit(void)
//{
//	OCInitType TIM_OCInitStructure;
//    GPIO_InitType GPIO_InitStructure;
//    ULONG ulPrescalerValue = 0;
//	TIM_TimeBaseInitType TIM_TimeBaseStructure;

//    GPIO_InitStruct(&GPIO_InitStructure);
//    /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
//    GPIO_InitStructure.Pin        = GPIO_PIN_15;
//    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
//    GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
//    GPIO_InitStructure.GPIO_Alternate = GPIO_AF5_TIM2;
//    GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);

  
//    /* TIM2 clock enable */
//    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
    
//    /* Compute the prescaler value to have TIMx counter clock equal to 100 kHz */
//    ulPrescalerValue = (ULONG)(SystemCoreClock / 100000) - 1;
//    /* Time base configuration */
//	TIM_TimeBaseStructure.Period    = 100 - 1;	/*1MS*/
//    TIM_TimeBaseStructure.Prescaler = ulPrescalerValue;
//    TIM_TimeBaseStructure.ClkDiv    = 0;
//    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;
//    TIM_InitTimeBase(TIM2, &TIM_TimeBaseStructure);
//    /* Prescaler configuration */
//    TIM_ConfigPrescaler(TIM2, ulPrescalerValue, TIM_PSC_RELOAD_MODE_IMMEDIATE);

//    TIM_InitOcStruct(&TIM_OCInitStructure);    
//    TIM_OCInitStructure.OcMode      = TIM_OCMODE_PWM1;
//    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
//    TIM_OCInitStructure.Pulse       = 80;
//    TIM_OCInitStructure.OcPolarity  = TIM_OC_POLARITY_LOW;

//    TIM_InitOc1(TIM2, &TIM_OCInitStructure);

//    TIM_ConfigOc1Preload(TIM2, TIM_OC_PRE_LOAD_ENABLE);

//    TIM_ConfigArPreload(TIM2, ENABLE);
//    /* TIM3 enable counter */
//    TIM_Enable(TIM2, DISABLE);

//	//TIM_EnableCapCmpCh(TIM2, TIM_CH_1 , TIM_CAP_CMP_DISABLE);
//}

